// pages/report/report.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isAllSelect: false,
    haschose: false,
    arr:[],
    chose:true,
    pad:"0px",
    bianji:'编辑',
    report:[
      {
        src:"",
        name:"",
        date:"",
        mes:"",
        status:false
      }
    ]
  },
  start_chose:function(){
    var that = this;
    var bianji = this.data.bianji;
    var report = that.data.report;
    if (bianji == '编辑') {
      that.setData({
        pad:'60rpx',
        bianji:'完成',
        chose:false
      })
    } else {
      that.setData({
        pad: '0rpx',
        bianji: '编辑',
        chose: true
      })
    }
  },
  //选择
  switchSelect: function (e) {
    let that = this;
    var haschose = that.data.haschose;
    var index = e.currentTarget.dataset.index;
    var report = that.data.report;
    var arr = that.data.arr;
    report[index].status = !report[index].status
    arr = arr.concat(report[index])
    that.setData({
      report : report,
      arr : arr
    })
    console.log(that.data.report)
  },
  //全选
  allSelect: function (e) {
    let that = this;
    var haschose = that.data.haschose;
    var report = that.data.report;
    var isAllSelect = !that.data.isAllSelect;
    if (isAllSelect) {
      for (var i = 0; i < report.length; i++) {
        console.log(i)
        report[i].status = true
      }
      that.setData({
        report: report,
      })
      //console.log(that.data.report)
    } else {
      for (var i = 0; i < report.length; i++) {
        report[i].status = false
      }
      that.setData({
        report: report,
      })
    }
    that.setData({
      isAllSelect: isAllSelect
    })
  },
  //删除
  delete: function (e) {
    let that = this
    var arr = that.data.arr
    console.log(arr)
    var report = that.data.report
    // for(var i = 0; i < report.length; i++){

    //   if (that.data.isAllSelect){
    //     report.splice(0, report.length)
    //   }
    //   else if(report[i].status == true){
    //     for(var n = 0; n < arr.length; n++){
    //       report.splice(i,1)
    //     }
    //     that.setData({
    //       arr:[]
    //     })
    //   }
    //   // else {
    //   //   let that = this;
    //   //   var report = that.data.report;
    //   //   var arr = that.data.arr

    //   //   wx.request({
    //   //     url: app.data.Url +'Cart/delete',
    //   //     data: {
    //   //       cartid: arr,
    //   //       uid:wx.getStorageSync('userInfo').uid
    //   //     },
    //   //     method: 'get',
    //   //     success: function (res) {
    //   //       console.log(res.data.data[0])
    //   //       that.setData({
    //   //         report: res.data.data,
    //   //         arr: arr, 
    //   //         price:0
    //   //       })
    //   //     }
    //   //   })
    //   // }
    // }
    // that.setData({
    //   report: report,
    // });
    var commid = '';
    for (var i = 0; i < report.length; i++) {
      if (that.data.isAllSelect) {
        commid += report[i]['commid'] + ','
      } else if (report[i].status == true) {
        commid += report[i]['commid'] + ','
      }
    }
    wx.request({
      url: app.url + '/User/evaluation_del',
      method: 'post',
      data: {
        commid: commid,
        uid:wx.getStorageSync('uid')
      },
      success: function (res) {
        console.log(res.data)
        that.setData({})
      }
    })
  },
  onLoad: function (options) {
  
  },
  onReady: function () {
  
  },
  onShow: function () {
    var that = this ;
    var uid  = wx.getStorageSync('uid') ;
    wx.request({
      url     : app.url + '/User/evaluation_list',
      method  : 'post',
      data    : {uid : uid},
      success: function (res) { 
        if(res.data.code == 1){
          that.setData({ report : res.data.data })
        }else{
          that.setData({ report : [] })
          wx.showToast({ title  : '暂无评论'})
        }
      },
      error   : function(){}
    })
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
  
  }
})